<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
import { getUrl,confirmModal, ptFloat, minusF } from '@/static/js/common.js';
import {buildFormData, formValidation, tranferToEditors, changeEditor, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad,getWorkCacheKey} from '@/static/js/formUtil.js';
import urlUtil from '@/static/js/urlUtil.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return buildFormData({
			initValue: {
				refereeMobile:'',
				interviewUserId:''
			},
			candidateCacheKey:'hr-u-interview-candidate-cache',
			refereeCacheKey:'hr-u-interview-referee-cache',
			interviewOffCacheKey:'hr-u-interview-interviewOff-cache',
			userIdsCacheKey:'hr-u-interview-userIds-cache'
		})
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		getEduList(tcandidateId){
			let _that = this
			_that.om.request(
				"trecruitAgencyInterviewEvaluate/setInfo",
				{
					tcandidateId:tcandidateId
				}
			).then(res=>{
				_that.subEditorObj["title3"].workList = res.value.eduList
			})
		},
		formSubmit() {
			if(this.checkedit){
				this.om.formSave('commonForm/doCheckOrApproveUpdate')
			}else{
				this.om.formSave('trecruitAgencyInterviewEvaluate/addTrecruitAgencyInterviewEvaluate')
			}
		},
		initLoad(options) {
			let _that = this;
			var fileId = options.fileId
			var ajaxArr = [
				urlUtil.zposUrl(options.code), 
				urlUtil.sysdictComboboxUrl('23586'),//目前状态
				urlUtil.sysdictComboboxUrl('23590'),//招聘信息途径
				{
					url:"temployee/getRoleCategoryList", //角色类别
				}
			];
			if(fileId){
				ajaxArr.push({
					url:"trecruitAgencyInterviewEvaluate/getMobileInfo",
					param:{id:fileId}
				})
			}
			let checkedit = (options.checkedit == 'true')
			if(checkedit){
				ajaxArr.push(urlUtil.dictZeroComboboxUrl('36802'))
			}
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'),mainData);
				changeEditor('title8', 'currentStatusId', 'data', configSelectData(res[1], 'id', 'dicname'),mainData);
				changeEditor('title8', 'recruitInfoApproachId', 'data', configSelectData(res[2], 'id', 'dicname'),mainData);
				if(res[3].length>1){
					changeEditor("title8","roleCategoryId",'data',configSelectData(res[3],"id","dicname"),mainData)
				}else{
					changeEditor("title8","roleCategoryId",'data',configSelectData(res[3],"id","dicname",true),mainData)
				}
				changeEditor('title8', 'arriveTime', {
					suffixCol:'3'
				}, mainData);
				changeEditor('title1','tcandidateId',{
					link:"../../public/search/search",
					linkParam: {
						other:JSON.stringify({
							editId:fileId
						}),
						initload: true,
						namefield: 'infoname',
						extrafield:'posName',
						url: "trecruitAgencyInterviewEvaluate/getUserList",
						cachename: _that.candidateCacheKey,
						placeholder: '请输入姓名'
					}
				}, null, mainData);
				//推荐人
				changeEditor('title8','refereeId',{
					link:"../../public/pageSearch/pageSearch",
					linkParam: {
						searchfield:'q',
						namefield: urlUtil.userPageUrl().namefield,
						url: urlUtil.userPageUrl().url,
						cachename: _that.refereeCacheKey,
						placeholder: urlUtil.userPageUrl().prompt
					}
				}, null, mainData);
				//面试负责人
				changeEditor('title13','interviewOffId',{
					link:"../../public/pageSearch/pageSearch",
					linkParam: {
						searchfield:'q',
						namefield: urlUtil.officePageUrl().namefield,
						url: urlUtil.officePageUrl().url,
						cachename: _that.interviewOffCacheKey,
						placeholder: urlUtil.officePageUrl().prompt
					}
				}, null, mainData);
				//参与评价人员
				changeEditor('title13','userIds',{
					linkParam: {
						pagination:true,
						searchfield:'q',
						namefield: urlUtil.userPageUrl().namefield,
						url: urlUtil.userPageUrl().url,
						cachename: _that.userIdsCacheKey,
						placeholder: urlUtil.userPageUrl().prompt
					}
				}, null, mainData);
				if(_that.subEditorObj["title15"]){
					changeEditor("title15", "objStr1", {
						titleTemplate:"评价要素：<%= interviewElementName || '未填写'%>",
						subtitle1Template:"分数：<%= score || '未填写'%>",
					}, null, mainData)
				}
				if(_that.subEditorObj["title3"]){
					changeEditor("title3","title3_edu",{
						showAdd:false,
						showDel:false,
						showArrow:false,
						titleTemplate:"开始时间:<%=startDateE || '未填写'%>"+'~'+"结束时间:<%=endDateE || '未填写'%>",
						subtitle1Template:"学历:<%=eduEName || '未填写'%>"+'|'+"院校名称:<%=schoolE || '未填写'%>"+'|'+"专业:<%=studyDirectEName || '未填写'%>",
						subtitle2Template:"学习形式:<%=studyWayName || '未填写'%>"+'|'+"证书号:<%=certificateE || '未填写'%>"+'|'+"学习结束类型:<%=learningEndTypeName|| '未填写'%>",
						subtitle3Template:"学制:<%=learningLengthTypeName || '未填写'%>"+'|'+"学位:<%=degreeName || '未填写'%>",
					},null,mainData)
				}
				if(fileId) {
					let fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					for(let i in res[4].value.trecruitAgencyInterviewEvaluate) {
						if(fieldArr.includes(i)) {
							let value = res[4].value.trecruitAgencyInterviewEvaluate[i] || res[4].value.userInfoResponse[i]
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, value)
							_that.$set(_that.formdata, i, value)
						}
					}
					
					//评价要素
					if(_that.subEditorObj["title15"]){
						let scoreResponses = _that._.cloneDeep(res[4].value.scoreResponses)
						_that.subEditorObj["title15"].workList = scoreResponses
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr1", JSON.stringify(scoreResponses))
						_that.$set(_that.formdata, "objStr1", JSON.stringify(scoreResponses))
						delete res[4].value.scoreResponses
						if(checkedit){
							changeEditor('title17', 'resultCode', 'data', configSelectData(res[5], 'code', 'dicname'), mainData);
						}
					}
					//修改时初始化下拉框的值
					_that.$nextTick(function(){
						if(this.$refs.autoform){
							this.$refs.autoform.initSelectIndex('topinfo', 'zposId')
							this.$refs.autoform.initSelectIndex('title8', 'currentStatusId')
							this.$refs.autoform.initSelectIndex('title8', 'recruitInfoApproachId')
							this.$refs.autoform.initSelectIndex('title17', 'resultCode')
							this.$refs.autoform.initSelectIndex("title8","roleCategoryId")
							this.getEduList(this.$refs.autoform.formdata.tcandidateId)
						}
					})
				}
			},options)
		},
	},
	onLoad(options) {
		let _that = this;
		_that.initLoad(options);
	},
	onUnload(){
		//#ifdef APP-PLUS
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				uni.removeStorage({
					key: getWorkCacheKey(fieldId)
				});
			}
		}
		//#endif
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key: _that.candidateCacheKey,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.tcandidateId = d.id
						_that.getEduList(d.id)
						formdata.userName = d.infoname
						formdata.posId = d.posId
						formdata.posName = d.posName
						formdata.instId = d.instId
						formdata.instName = d.instName
						uni.removeStorage({
							key: _that.candidateCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.refereeCacheKey,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.refereeId = d.id
						formdata.refereeName = d.username
						formdata.refereeMobile = d.mobile
						uni.removeStorage({
							key: _that.refereeCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.interviewOffCacheKey,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.interviewOffId = d.id
						formdata.interviewUserId = d.userId
						formdata.interviewUserName = d.userName
						uni.removeStorage({
							key: _that.interviewOffCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.userIdsCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.userIds = d.map(e=>e.id).join(",")
						formdata.userNames = d.map(e=>e.username).join(",");
						_that.$refs.autoform.changeEditor('title13','userIds',{
							initSelect:d
						})
						uni.removeStorage({
							key: _that.userIdsCacheKey
						});
					}
				}
			});
		}
	}
};
</script>
<style></style>
